package org.wxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.wxl.domain.userEntity;
import org.wxl.domain.vo.LoginUser;
import org.wxl.mapper.UserMapper;

import java.util.Objects;

/**
 * @author 王学良
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息，连接数据库
        QueryWrapper<userEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        userEntity userEntity = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(userEntity))
        {
            throw new RuntimeException("用户名或密码错误");
        }
        //判断密码是否正确
        //todo
        //返回UserDetails对象
        return new LoginUser(userEntity);
    }
}
